package com.ecloud.gardensterminal.server.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ecloud.gardensterminal.constant.DeviceTypeConstant;
import com.ecloud.gardensterminal.controller.dto.GardensDeviceSelectDto;
import com.ecloud.gardensterminal.dao.GardensDeviceMapper;
import com.ecloud.gardensterminal.dao.GardensSleetMapper;
import com.ecloud.gardensterminal.domain.GardensDevice;
import com.ecloud.gardensterminal.domain.GardensSleet;
import com.ecloud.gardensterminal.server.GardensSleetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Squbi
 */
@Service
public class GardensSleetServiceImpl extends ServiceImpl<GardensSleetMapper, GardensSleet> implements GardensSleetService {
    @Autowired
    private GardensSleetMapper sleetMapper;
    @Autowired
    private GardensDeviceMapper deviceMapper;

    @Override
    public List<GardensSleet> selectList(GardensDeviceSelectDto dto) {
        dto.setPageIndex(dto.getPageSize() * (dto.getPageIndex() - 1));
        return sleetMapper.selectPageByDto(dto);
    }

    @Override
    public List<GardensSleet> selectNewly() {
        List<GardensDevice> gardensDevices = deviceMapper.selectByDeviceType(DeviceTypeConstant.SLEET);
        List<GardensSleet> result = new ArrayList<>();
        for (GardensDevice gardensDevice : gardensDevices) {
            List<GardensSleet> gardensSleets = sleetMapper.selectPageByDto(new GardensDeviceSelectDto(gardensDevice.getDeviceId(), 1, 1));
            result.addAll(gardensSleets);
        }
        return result;
    }
}
